/*--------------------------------------------------------------------------------*/
#ifndef __INPUT_FILE__
#define __INPUT_FILE__
/*--------------------------------------------------------------------------------*/
#include "primitives.h"
#include "object.h"
#include "util.h"
#include "debug.h"
#include <cstdio>
/*--------------------------------------------------------------------------------*/
#define MAXIMUM_LINE_LENGTH 	1024
/*--------------------------------------------------------------------------------*/
class INPUT_FILE : public OBJECT
{

	// -- PUBLIC

public:

    DECLARE_REF( INPUT_FILE );

    // .. CONSTRUCTORS

    INPUT_FILE() : OBJECT(), File( NULL ), FilePath(), FileName(), FileBaseName(), FileExtension()
    {

    }

    INPUT_FILE( const TEXT & file_path ) : OBJECT(), File( NULL ), FilePath( file_path ), FileName(), FileBaseName(), FileExtension()
    {
    	Initialize();
    }

    virtual ~INPUT_FILE()
    {
     
    }

    // .. METHODS

    BOOL Open( const TEXT & file_path )
    {
    	if( ( File = fopen( file_path.GetArray(), "rb" ) ) )
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }

    BOOL Open()
    {
    	DEBUG_Require( FilePath != "" );

    	if( ( File = fopen( FilePath.GetArray(), "rb" ) ) )
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }

    BOOL IsOpen() const
    {
    	return File != NULL;
    }

    VOID Close()
    {
    	fclose( File );
    }

    template< typename _TYPE_ >
    const _TYPE_ Read()
    {
    	_TYPE_
			result;

    	fread( &result, 1, sizeof( _TYPE_ ), File );

    	return result;
    }

    template< typename _TYPE_ >
    VOID Read( _TYPE_ & dest )
    {
    	fread( &dest, 1, sizeof( _TYPE_ ), File );
    }

    VOID ReadBuffer( VOID * dest, const COUNT byte_count )
    {
    	fread( dest, byte_count, 1, File );
    }

    BOOL ReadLine( TEXT & dest )
    {
    	CHAR
			buffer[ MAXIMUM_LINE_LENGTH ];

    	DEBUG_Require( IsOpen() );

    	if ( fgets( buffer, MAXIMUM_LINE_LENGTH, File ) )
    	{
    		dest = buffer;
    		dest.Trim();
    		return true;
    	}

		return false;
    }

    TEXT GetContent()
    {
    	TEXT
			buffer,
			result;

    	result = "";

    	while( ReadLine( buffer ) )
    	{
    		result += buffer + "\n";
    	}

    	return result;
    }

    VOID Initialize()
    {
    	FileName = FilePath.SubString( FilePath.FindLast( '/' ) + 1 );
    	FileBaseName = FileName.SubString( 0, FileName.FindLast( '.' ) );
    	FileExtension = FileName.SubString( FileName.FindLast( '.' ) + 1);
    }

    VOID SetFilePath( const TEXT & file_path )
    {
    	FilePath = file_path;
    	Initialize();
    }

    const TEXT & GetFilePath()
    {
    	return FilePath;
    }

    const TEXT & GetFileName()
    {
    	return FileName;
    }

    const TEXT & GetBaseName()
    {
    	return FileBaseName;
    }

    const TEXT & GetExtension()
    {
    	return FileExtension;
    }

    // -- PRIVATE

private :

    // .. ATTRIBUTES

    FILE
		*File;
    TEXT
		FilePath,
		FileName,
		FileBaseName,
		FileExtension;
    
};
/*--------------------------------------------------------------------------------*/
#endif
